/**
 * Source file: Retrenchment Claim_AutoLookups.ejs
 * Copyright (C) 2013 : Quantum Solutions
 * Id: $Id$
 * Date: $Date$
 */
 
//Turn on your selected Environment before exporting your form from TeleForm.
// serverName is also defined in ConnectWebService (should be the same value)
//var serverName = "http://127.0.0.1:50057"; // rob VS
//var serverName = "http://WIN-OV9FIKDJ4BV:88"; // Willie test
//var serverName = "http://suglifeabiqa01:88"; // Nedbank test ACBVAL
//var serverName2 = "http://suglifeabiqa01:89"; // NedBank test Suburb
var serverName = "Http://2008-4.quantumsolutions.co.za:91"; // Quantum Cloud Test acbval Autocompletes
var serverName2 = "http://2008-4.quantumsolutions.co.za:92";// Quantum Cloud Test Suburb AutoCompletes 

$(document).ready(function() {

/*

Retrenchment Claim Form Autocomplete Field List
f76 Position Held at Previous Employer
f139 Position Held at previous Employer
f53 suburb
f55 City
f57 Postal Code
*/
	// the field id is generated by Electric paper and should be changed to the apporiate value 
	// if the form is changed (fields added or deleted) this id could also change
	// the best way to locate the id is to right click on the field in firefox and select "Inspect Element"

//Address Information
$("#f55").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f53").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f55').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f57').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f57").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f55').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f53').value = $.trim(pieces[pieces.length-1]); }		
		});		

		
$("#f76").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Occupation"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		// no select function
		});		

$("#f139").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Occupation"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		// no select function
		});	
});